package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ClassInfo;
import cn.wolfcode.crm.query.ClassInfoQuery;
import cn.wolfcode.crm.service.IClassInfoService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/classInfo")
@Service
public class ClassInfoController {

    @Autowired
    private IClassInfoService classInfoService;

    @Autowired
    private IEmployeeService employeeService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    @RequiresPermissions(value = {"classInfo:list", "班级列表"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") ClassInfoQuery qo) {
        //查教师信息
        model.addAttribute("employees", employeeService.listAll());
        //调用service的方法的到结果集
        PageInfo result = classInfoService.query(qo);

        //共享结果集：在页面遍历显示
        model.addAttribute("result", result);
        return "/classInfo/list"; //返回视图名称：加上前缀和后缀的ftl
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"classInfo:saveOrUpdate", "班级新增/编辑"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate( ClassInfo classInfo,Long employee_id) {

        System.out.println("-==============================---======================="+classInfo);

        if (classInfo.getId() == null) { // 代表新增
            classInfoService.save(classInfo, employee_id);
        } else { // 代表修改
            classInfoService.update(classInfo, employee_id);
    }
        return new JsonResult();
    }


    @RequestMapping("/delete")
    @RequiresPermissions(value = {"classInfo:delete", "班级删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        classInfoService.delete(id);
        return new JsonResult();
    }
}
